/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.editpart.connection;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.gef.EditPart;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.editpart.LinkNameDisplayEditPart;
import com.cea.papyrus.core.figures.MiddleEndpointLocator;
import com.cea.papyrus.diagraminterchange2.di2.GraphEdge;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;

/**
 * Edit part represented by a graph edge that representes a named element (ex: dependency).
 */
public class NamedGraphEdgeEditPart extends TaggedGraphEdgeEditPart {

	/**
	 * Name child edit part.
	 */
	protected LinkNameDisplayEditPart theNameLabel; 

	/**
	 * Creates a new NamedGraphEdgeEditPart.
	 * 
	 * @param graphEdge 
	 * @param modelManager 
	 */
	public NamedGraphEdgeEditPart(GraphEdge graphEdge, ModelManager modelManager) {
		super(graphEdge, modelManager);
	}

	/**
	 * Creates a new NamedGraphEdgeEditPart, with a tag on the link.
	 * 
	 * @param graphEdge 
	 * @param modelManager 
	 * @param tag 
	 */
	public NamedGraphEdgeEditPart(GraphEdge graphEdge, ModelManager modelManager, String tag) {
		super(graphEdge, modelManager, tag);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractEditPart#createChild(java.lang.Object)
	 */
	/**
	 * 
	 * 
	 * @param model 
	 * 
	 * @return 
	 */
	@Override
	protected EditPart createChild(Object model) {
		if( model instanceof GraphNode) {
			// index in contained edge = 0
			GraphNode element = (GraphNode)getModelChildren().get(0);	// getModelChildren only returns GraphNodes
			if((((GraphNode)model) == ((GraphNode)element)) ) {
				theNameLabel =new LinkNameDisplayEditPart(
						(GraphNode)element,
						this.getUmlElement(),
						modelManager);
				return theNameLabel;
			}
		}
		return super.createChild(model);
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.editpart.GraphEdgeEditPart#refreshVisuals()
	 */
	/**
	 * 
	 */
	@Override
	protected void refreshVisuals() {
		super.refreshVisuals();

		if(theNameLabel != null) {
			theNameLabel.refresh();

			//calculate middle point of the label
			org.eclipse.draw2d.geometry.Point middleLabel =new org.eclipse.draw2d.geometry.Point(
					theNameLabel.getGraphNode().getPosition().x-((Label) theNameLabel.getFigure()).getPreferredSize().width/2,
					theNameLabel.getGraphNode().getPosition().y-((Label) theNameLabel.getFigure()).getPreferredSize().height/2
			);
			//caluclate middle point of the link
			MiddleEndpointLocator lo =new MiddleEndpointLocator ((PolylineConnection)this.getFigure(),
					middleLabel);

			IFigure f=theNameLabel.getFigure();
			f.setLocation(theNameLabel.getGraphNode().getPosition());//set label a the good position
			if (theNameLabel.isVisible()){
				this.getContentPane().getLayoutManager().setConstraint(theNameLabel.getFigure(),lo);
			}
		}
	}
}
